package com.yehuishou.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.News;
import com.yehuishou.pojo.NewsParameter;
import com.yehuishou.service.NewsService;

/**
 * 
 * @author 杨斌
 *
 */

@Controller
public class NewsController {
	@Resource
	private NewsService newsservice;

	// 展示所有新闻
	@RequestMapping("newssel")
	public String newssel(@RequestParam(required = true, defaultValue = "1") Integer pageNum,
			@RequestParam(required = true, defaultValue = "5") Integer pageSize, NewsParameter np,
			HttpServletRequest request) {

		PageInfo<News> pageinfo = newsservice.newssel(pageNum, pageSize, np);
		request.setAttribute("pageinfo", pageinfo);
		request.setAttribute("np", np);
		return "news";
	}

	// 添加新闻跳转
	@RequestMapping("newsaddtz")
	public String newsaddtz() {
		return "news-add";
	}

	// 添加新闻
	@RequestMapping("newsadd")
	public String newsadd(News news) {
		Date d = new Date();
		news.setCreated(d);
		int i = newsservice.newsadd(news);
		if (i > 0) {
			return "redirect:newssel";
		}
		return "redirect:newssel";
	}

	// 删除新闻
	@ResponseBody
	@RequestMapping("newsdele")
	public int newsdele(Integer nid) {
		int i = newsservice.newsdele(nid);
		if (i > 0) {
			return 1;
		}
		return 2;
	}

	// 修改新闻跳转
	@RequestMapping("newsupdatetz")
	public String newsupdatetz(Integer nid, HttpServletRequest request) {
		List<News> list = newsservice.newsupdatetz(nid);
		request.setAttribute("list", list);
		return "news-update";
	}

	// 修改新闻跳转
	@RequestMapping("newsupdate")
	public String newsupdate(News news) {
		int i = newsservice.newsupdate(news);
		if (i > 0) {
			return "redirect:newssel";
		}
		return "redirect:newssel";
	}

}
